/*
 *  Autores: Claudio Djohnnatha, David Guilherme, Jailson Lima e Jordan Lira.
 *
 *  Projeto da disciplina Linguagem de Programação 2.
 *
 *  Arquivo: ActiveMessage.java
 *
 *  Data: 27/08/2013
 *
 */



import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Font;

public class ActiveMessage extends Thread
{
   private MessageLabel messageLabel;
   private String message;

   public ActiveMessage( MessageLabel ml, String m )
   {
      messageLabel = ml;
      message = m;
   }

   public void run()
   {
      synchronized ( messageLabel )
      {
         // Passa a mensagem do servidor para o editor.
         messageLabel.setMessage( message );

         try
         {
            int i = 0;
            while ( i < 46 )
            {
               i++;
               messageLabel.setLocation( messageLabel.getX() + ( (int) ( -0.773*i + 0.018*i*i ) ), messageLabel.getY() );
               Thread.sleep( 10 );
            }

            Thread.sleep( 2000 );

            i = 0;
            while ( i < 46 )
            {
               i++;
               messageLabel.setLocation( messageLabel.getX() + ( (int) ( 0.773*i - 0.018*i*i ) ), messageLabel.getY() );
               Thread.sleep( 10 );
            }

            messageLabel.setMessage( "" );

         } catch ( InterruptedException e ) {}
      }
   }
}

class MessageLabel extends JLabel
{
   private JTextField text;

   public MessageLabel()
   {
      setLayout( null );
      setSize( 196, 40 );

      text = new JTextField();
      text.setBounds( 10, 6, 176, 30 );
      text.setForeground( new Color( 255, 255, 255 ) );
      text.setFont( new Font( "Arial", Font.PLAIN, 12 ) );
      text.setBorder( BorderFactory.createEmptyBorder( 1, 1, 1, 1 ) );
      text.setEditable( false );
      text.setFocusable( false );
      text.setOpaque( false );
      add( text );
   }

   public void paintComponent( Graphics g )
   {
      super.paintComponent( g );

      g.setColor( new Color( 68, 175, 51 ) );
      g.fillRect( 0, 0, 196, 40 );
   }

   public void setMessage( String message )
   {
      text.setText( message );
   }

   public String getMessage()
   {
      return text.getText();
   }
}